package com.algorithm.thePrimaryAlgorithm;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

class ListNode22 {
    int val;
    ListNode22 next;

    ListNode22() {
    }

    ListNode22(int val) {
        this.val = val;
    }

    ListNode22(int val, ListNode22 next) {
        this.val = val;
        this.next = next;
    }
}

/**
 *
 */
@Slf4j
public class LinkSulotionLC22 {
    public ListNode22 removeNthFromEnd(ListNode22 head, int n) {
        int lenght = 0;
        ListNode22 head1 = head;
        while (head1 != null) {
            lenght++;
            head1 = head1.next;
        }
        int last = lenght - n;
        if (last == 0) {
            return head.next;
        }
        ListNode22 per = head;
        for (int i = 1; i < last; i++) {
            per = per.next;
        }
        per.next = per.next.next;
        return head;
    }

    @Test
    public void test(){
        ListNode22 l1 = new ListNode22(12);
        ListNode22 l2 = new ListNode22(2);
        ListNode22 l3 = new ListNode22(1);
        l1.next = l2;
        l2.next = l3;

        ListNode22 listNode22 = removeNthFromEnd(l1, 3);
        log.info("this is test {}",listNode22.val);
    }
}
